<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/6/27
 * Time: 3:53 PM
 */
namespace app\backend\modules\content\controllers;

use app\backend\controllers\BaseController;
use app\backend\modules\content\services\PageService;
use app\backend\modules\sys\services\ThemeService;
use app\common\models\Page;
use app\common\models\ThemeFile;
use Illuminate\Support\Facades\DB;


class PageController extends BaseController
{
    public function index(){
        return view("backend.content.page.index");
    }
    public function getList(){
        $search = request()->search;
        $list = (new PageService())->getList($search);
        return $this->successJson('',$list);
    }
    public function changeStatus()
    {
        $id = request()->id;
        $model = Page::site()->select()
            ->where('id',$id)
            ->first();
        if (empty($model)) {
            return $this->errorJson('未找到数据');
        }
        $status=Page::STATUS_YES;
        if($model->is_publish==Page::STATUS_YES){
            $status= Page::STATUS_NO;
        }
        $model->is_publish = $status;
        $model->save();
        return $this->successJson();
    }
    public function detail(){
        return view("backend.content.page.detail");
    }
    public function getDetail(){
        $id = intval(request()->id);
        $res['info'] = [];
        $res['publish_time'] =  date('Y-m-d H:i:s');
        if($id){
            $model = Page::site()->select()
                ->where('id',$id)
                ->first();
            if (empty($model)) {
                return $this->errorJson('未找到数据');
            }
            $model->publish_time = date('Y-m-d H:i:s',$model->publish_time);
            $model->thumb = fmt_media_url($model->thumb);
            $res['info'] = $model;
        }
        $themeService = new ThemeService();
        $themeFiles = $themeService->getDefaultTypeFiles();
        $res['pc_detail_tpl_list'] = $themeFiles['pc'][ThemeFile::TYPE_PAGE];
        $res['h5_detail_tpl_list'] = $themeFiles['h5'][ThemeFile::TYPE_PAGE];
        return $this->successJson('',$res);
    }
    public function create(){
        $data = request()->params;
        $data['site_id'] = \YunCms::siteId();
        if(!$data['publish_time']){
            //默认当前时间
            $data['publish_time'] = time();
        }else{
            $data['publish_time'] = strtotime($data['publish_time']);
        }
        $model = new Page();
        $model->fill($data);
        $validator = $model->validator();
        if ($validator->fails()) {
            return $this->errorJson($validator->messages());
        } else {
            $model->save();
            return $this->successJson();
        }
    }
    public function update(){
        $data = request()->params;\Log::debug('$params',$data);
        $id = $data['id'];
        if(!$data['publish_time']){
            //默认当前时间
            $data['publish_time'] = time();
        }else{
            $data['publish_time'] = strtotime($data['publish_time']);
        }
        $model = Page::find($id);
        if (empty($model)) {
            $this->errorJson('未找到数据');
        }
        \Log::debug('$data',$data);
        $model->fill($data);
        $validator = $model->validator();
        if ($validator->fails()) {
            return $this->errorJson($validator->messages());
        } else {
            $model->save();
            return $this->successJson();
        }
    }
    public function delete()
    {
        $id = request()->id;
        $model = Page::find($id);
        if(!$model){
            return $this->errorJson('未找到数据');
        }
        $model->delete();
        return $this->successJson();
    }
    /**
     * 检索页面
     */
    public function query(){
        $kwd = trim(request()->kwd);
        $list = Page::site()->select(['id','title']);
        if($kwd){
            $list = $list->where('title','like','%'.$kwd.'%');
        }
        $list = $list
            ->orderBy('id', 'desc')
            ->take(100)
            ->get();
        return $this->successJson('',$list);
    }
}